package edu.gu.hajo.gcl.script;

/**
 * Wrapperclass for the script code to execute
 * @author hajo
 * 
 */
public class Script {
	
	public final static char EOF = Character.MIN_VALUE;

	private int pos;

	private final char[] scriptStr;

	public Script(String script) {
		this.scriptStr = new char[script.length()];
		script.getChars(0, script.length(), scriptStr, 0);
		this.pos = 0;
	}

	public char next() {
		if (pos < scriptStr.length) {
			return scriptStr[pos++];
		} else
			return EOF; //Character.MIN_VALUE;
	}

	public Script putBack() {
		if (pos > 0) {
			pos--;
		}
		return this;
	}

	public boolean hasNext() {
		return pos < scriptStr.length;
	}

	@Override
	public String toString() {
		/*
		 * Sometimes helpful to see posititon
		 */
		String s = new String(scriptStr);
		String pre = s.substring(0, pos);
		String post = s.substring(pos, s.length());
		return pre + "!" + post;
	}

	// Used exceptions only for message
	public int getPos() {
		return pos;
	}
	
	public void first() {
		pos = 0;
	}
	
	public void last(){
		pos = scriptStr.length;
	}
}
